using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Awesome.Extensions.String
{
    public static class StringJoinEx
    {
        /// <summary>
        /// Joins strings using separator
        /// </summary>
        public static string Glue(this IEnumerable<string> strings, char separator)
        {
            var builder = new StringBuilder();
            using (var enumenator = strings.GetEnumerator())
            {
                if (!enumenator.MoveNext())
                    return builder.ToString();

                builder.Append(enumenator.Current);
                while (enumenator.MoveNext())
                {
                    builder.Append(separator);
                    builder.Append(enumenator.Current);
                }
            }

            return builder.ToString();
        }

        /// <summary>
        /// Joins strings using separator
        /// </summary>
        public static string Glue(this IEnumerable<string> strings, string separator)
        {
            var builder = new StringBuilder();
            using (var enumenator = strings.GetEnumerator())
            {
                if (!enumenator.MoveNext())
                    return builder.ToString();

                builder.Append(enumenator.Current);
                while (enumenator.MoveNext())
                {
                    builder.Append(separator);
                    builder.Append(enumenator.Current);
                }
            }

            return builder.ToString();
        }

        /// <summary>
        /// Joins elements using separator; ToString() will be applied to each of them
        /// </summary>
        public static string Glue<T>(this IEnumerable<T> sequence, char separator)
        {
            return sequence.Select(e => e.ToString()).Glue(separator);
        }

        /// <summary>
        /// Joins elements using separator; ToString() will be applied to each of them
        /// </summary>
        public static string Glue<T>(this IEnumerable<T> sequence, string separator)
        {
            return sequence.Select(e => e.ToString()).Glue(separator);
        }

        public static string Glue<T>(this IEnumerable<T> sequence)
        {
            return sequence.Select(e => e.ToString()).Glue(System.String.Empty);
        }

    }
}